package com.ztom.daily.n2022.m4;

import java.util.ArrayList;
import java.util.List;

/**
 * 字典序排数
 * <p>
 * https://leetcode-cn.com/problems/lexicographical-numbers/
 *
 * @author ZhangTao
 */
public class Day18LexicalOrder {

    public List<Integer> lexicalOrder(int n) {
        List<Integer> res = new ArrayList<>();
        for (int i = 0, j = 1; i < n; i++) {
            res.add(j);
            if (j * 10 <= n) {
                j *= 10;
            } else {
                while (j % 10 == 9 || j + 1 > n) {
                    j /= 10;
                }
                j++;
            }
        }
        return res;
    }
}
